﻿#region

using System;
using System.Drawing;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.CustomerPanel
{
    public partial class Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["f"]))
            {
                if (Request.QueryString["f"].Equals("lg"))
                {
                    FormsAuthentication.SignOut();
                    Response.Redirect("~/Login.aspx");
                }
            }
            if (!IsPostBack)
            {
                var districts = DistrictBLL.GetAllDistricts();
                ddlFromDistrict.DataTextField = "Name";
                ddlFromDistrict.DataValueField = "DistrictId";
                ddlFromDistrict.DataSource = districts;
                ddlFromDistrict.DataBind();
                LoadCities("ddlFromDistrict");
                ddlToDistrict.DataTextField = "Name";
                ddlToDistrict.DataValueField = "DistrictId";
                ddlToDistrict.DataSource = districts;
                ddlToDistrict.DataBind();
                LoadCities("ddlToDistrict");
                txtTravelDate.Text = DateTime.Now.ToShortDateString();
                LoadCarDDL();
            }
        }

        private void LoadCarDDL()
        {
            ddlCars.Items.Clear();
            for (var x = 0; x <= Convert.ToInt32(ddlVehicleNum.SelectedValue); x++)
            {
                ddlCars.Items.Add(new ListItem
                {
                    Text = x.ToString(),
                    Value = x.ToString()
                });
            }
            ddlCars.SelectedValue = ddlVehicleNum.SelectedValue;
            ddlVans.SelectedValue = "0";
        }

        protected void btnTravelCalender_OnClick(object sender, EventArgs e)
        {
            TravelCalendar.Visible = !TravelCalendar.Visible;
        }

        protected void TravelCalendar_OnSelectionChanged(object sender, EventArgs e)
        {
            txtTravelDate.Text = TravelCalendar.SelectedDate.ToShortDateString();
            TravelCalendar.Visible = false;
        }

        protected void ddlFromDistrict_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCities("ddlFromDistrict");
            txtFromCity.Text = "";
            txtFromCity.Visible = false;
        }

        private void LoadCities(string id)
        {
            if (id.Equals("ddlFromDistrict"))
            {
                var cities = CityBLL.GetAllCitiesByDistrictId(Convert.ToByte(ddlFromDistrict.SelectedValue));
                ddlFromCity.DataTextField = "Name";
                ddlFromCity.DataValueField = "CityId";
                ddlFromCity.DataSource = cities;
                ddlFromCity.DataBind();
                ddlFromCity.Items.Add(new ListItem
                {
                    Text = "Other",
                    Value = "Other"
                });
            }
            else if (id.Equals("ddlToDistrict"))
            {
                var cities = CityBLL.GetAllCitiesByDistrictId(Convert.ToByte(ddlToDistrict.SelectedValue));
                ddlToCity.DataTextField = "Name";
                ddlToCity.DataValueField = "CityId";
                ddlToCity.DataSource = cities;
                ddlToCity.DataBind();
                ddlToCity.Items.Add(new ListItem
                {
                    Text = "Other",
                    Value = "Other"
                });
            }
        }

        protected void ddlToDistrict_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCities("ddlToDistrict");
            txtToCity.Text = "";
            txtToCity.Visible = false;
        }

        protected void ddlFromCity_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            txtFromCity.Visible = ddlFromCity.SelectedValue.Equals("Other");
            if (txtFromCity.Visible) txtFromCity.Text = "";
        }

        protected void ddlToCity_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            txtToCity.Visible = ddlToCity.SelectedValue.Equals("Other");
            if (txtToCity.Visible) txtToCity.Text = "";
        }

        protected void btnUpdate_OnClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    var originTime = Convert.ToDateTime(txtTravelDate.Text + " " + ddlFromTimeHour.SelectedValue +
                                                        ":" + ddlFromTimeMinute.SelectedValue + " " +
                                                        ddlFromAmPm.SelectedValue);
                    if (ddlType.SelectedValue.Equals("PICK"))
                    {
                        var booking = new PickBooking
                        {
                            CustomerId = Convert.ToInt32(User.Identity.Name),
                            Type = ddlType.SelectedValue,
                            OriginDateTime = originTime,
                            Destination = (txtToCity.Visible) ? txtToCity.Text : ddlToCity.SelectedItem.Text
                        };
                        var location = new CurrentLocation
                        {
                            HomeNumberOrName = txtNameOrNum.Text,
                            Street = txtStreet.Text,
                            City = (txtFromCity.Visible) ? txtFromCity.Text : ddlFromCity.SelectedItem.Text,
                            DistrictId = Convert.ToInt32(ddlFromDistrict.SelectedValue)
                        };
                        var vehicleRequirement = new BookingVehicleRequirement
                        {
                            NumberOfCars = Convert.ToInt32(ddlCars.SelectedValue),
                            NumberOfVans = Convert.ToInt32(ddlVans.SelectedValue)
                        };
                        booking.OriginLocation = location;
                        booking.VehicleRequirement = vehicleRequirement;
                        PickBookingBLL.NewPickBooking(booking);
                    }
                    else
                    {
                        var booking = new PickAndDropBooking
                        {
                            CustomerId = Convert.ToInt32(User.Identity.Name),
                            Type = ddlType.SelectedValue,
                            OriginDateTime = originTime,
                            MidPoint = (txtToCity.Visible) ? txtToCity.Text : ddlToCity.SelectedItem.Text
                        };
                        var location = new CurrentLocation
                        {
                            HomeNumberOrName = txtNameOrNum.Text,
                            Street = txtStreet.Text,
                            City = (txtFromCity.Visible) ? txtFromCity.Text : ddlFromCity.SelectedItem.Text,
                            DistrictId = Convert.ToInt32(ddlFromDistrict.SelectedValue)
                        };
                        var vehicleRequirement = new BookingVehicleRequirement
                        {
                            NumberOfCars = Convert.ToInt32(ddlCars.SelectedValue),
                            NumberOfVans = Convert.ToInt32(ddlVans.SelectedValue)
                        };
                        booking.OriginLocation = location;
                        booking.VehicleRequirement = vehicleRequirement;
                        PickAndDropBookingBLL.NewPickAndDropBooking(booking);
                    }
                    ResetForm();
                    Response.Redirect("Booking.aspx");
                }
                catch
                {
                    lblStatus.Text = "Something went wrong";
                    lblStatus.ForeColor = Color.Red;
                }
            }
        }

        private void ResetForm()
        {
            ddlType.SelectedIndex = 0;
            LoadCarDDL();
            txtNameOrNum.Text = "";
            txtStreet.Text = "";
            txtToCity.Text = "";
            txtToCity.Visible = false;
            ddlFromDistrict.SelectedIndex = 4;
            LoadCities("ddlFromDistrict");
            txtToCity.Text = "";
            txtToCity.Visible = false;
            ddlToDistrict.SelectedIndex = 4;
            LoadCities("ddlToDistrict");
            txtTravelDate.Text = DateTime.Now.ToShortDateString();
        }

        protected void CustomValidatorFromCity_OnServerValidate(object source, ServerValidateEventArgs args)
        {
            if ((!txtFromCity.Visible) || (txtFromCity.Visible && (!string.IsNullOrEmpty(txtFromCity.Text))))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
                txtFromCity.Focus();
            }
        }

        protected void TravelCalendar_OnDayRender(object sender, DayRenderEventArgs e)
        {
            if (txtTravelDate.Text != "")
            {
                if (e.Day.Date < DateTime.Now.AddDays(-1) || e.Day.Date > DateTime.Now.AddDays(7))
                {
                    e.Day.IsSelectable = false;
                }
            }
        }

        protected void ddlVehicleNum_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCarDDL();
        }

        protected void ddlCars_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            ddlVans.SelectedValue =
                (Convert.ToInt32(ddlVehicleNum.SelectedValue) - Convert.ToInt32(ddlCars.SelectedValue)).ToString();
        }

        protected void btnReset_OnClick(object sender, EventArgs e)
        {
            ResetForm();
        }

        protected void CustomValidatorToCity_OnServerValidate(object source, ServerValidateEventArgs args)
        {
            if ((!txtToCity.Visible) || (txtToCity.Visible && (!string.IsNullOrEmpty(txtToCity.Text))))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
                txtToCity.Focus();
            }
        }
    }
}